/*
 * Copyright (c) 2017. 杭州端点网络科技有限公司.  All rights reserved.
 */

package com.jiantong.admin.controller;

import com.google.common.base.Strings;
import com.jiantong.user.model.BreedFactory;
import com.jiantong.user.model.ButcherFactory;
import com.jiantong.user.model.Government;
import com.jiantong.user.service.OrganizationWriteService;
import io.terminus.boot.rpc.common.annotation.RpcConsumer;
import io.terminus.common.exception.JsonResponseException;
import io.terminus.common.model.Response;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author : panxin
 */
@Slf4j
@RestController
@RequestMapping
public class AdminOrganizations {

    @RpcConsumer
    private OrganizationWriteService organizationWriteService;

    /**
     * 运营操作创建政府机构信息
     * @param government 政府信息
     * @return 新建ID
     */
    @RequestMapping(value = "/api/organization/create-government", method = RequestMethod.POST)
    public Long createGovernment(@RequestBody Government government) {
        checkGovernmentArguments(government);
        checkLocation(government.getProvinceId(), government.getCityId(), government.getRegionId());

        // TODO 如果有隶属于某个政府机构的下一级需要设置 level pid ...

        Response<Long> resp = organizationWriteService.createGovernment(government);
        if (!resp.isSuccess()) {
            log.error("failed to create government = {}", government, resp.getError());
            throw new JsonResponseException(resp.getError());
        }
        return resp.getResult();
    }

    /**
     * 运营操作创建屠宰场信息
     * @param factory 屠宰场信息
     * @return 新建ID
     */
    @RequestMapping(value = "/api/organization/create-butcher-factory", method = RequestMethod.POST)
    public Long createButcherFactory(@RequestBody ButcherFactory factory) {
        checkButcherFactoryArguments(factory);
        checkLocation(factory.getProvinceId(), factory.getCityId(), factory.getRegionId());

        Response<Long> resp = organizationWriteService.createButcher(factory);
        if (!resp.isSuccess()) {
            log.error("failed to create butcher factory = {}", factory, resp.getError());
            throw new JsonResponseException(resp.getError());
        }
        return resp.getResult();
    }

    /**
     * 运营操作新建养殖场信息
     * @param factory 养殖场信息
     * @return 新建ID
     */
    @RequestMapping(value = "/api/organization/create-breeder-factory", method = RequestMethod.POST)
    public Long createBreedFactory(@RequestBody BreedFactory factory) {
        checkBreedFactoryArguments(factory);
        checkLocation(factory.getProvinceId(), factory.getCityId(), factory.getRegionId());

        Response<Long> resp = organizationWriteService.createBreeder(factory);
        if (!resp.isSuccess()) {
            log.error("failed to create government = {}", factory, resp.getError());
            throw new JsonResponseException(resp.getError());
        }
        return resp.getResult();
    }

    // TODO 更新信息

    private void checkGovernmentArguments(Government government) {
        if (Strings.isNullOrEmpty(government.getName())) {
            log.error("government name is null");
            throw new JsonResponseException("government.name.is.null");
        }
    }

    private void checkButcherFactoryArguments(ButcherFactory factory) {
        if (Strings.isNullOrEmpty(factory.getName())) {
            log.error("butcher factory name is null");
            throw new JsonResponseException("butcher.factory.name.is.null");
        }
    }


    private void checkBreedFactoryArguments(BreedFactory factory) {
        if (Strings.isNullOrEmpty(factory.getName())) {
            log.error("breed factory name is null");
            throw new JsonResponseException("breed.factory.name.is.null");
        }
        if (factory.getIsScale() == null) {
            log.error("breed factory scale is null");
            throw new JsonResponseException("breed.factory.scale.is.null");
        }
        if (factory.getIsScale() == null && factory.getScale() == null) {
            log.error("breed factory scale type is null");
            throw new JsonResponseException("breed.factory.scale.type.is.null");
        }
        if (factory.getBreedType() == null) {
            log.error("breed factory breed type is null");
            throw new JsonResponseException("breed.factory.breed.type.is.null");
        }
    }

    private void checkLocation(Long provinceId, Long cityId, Long regionId) {
        if (provinceId == null) {
            log.error("province id is null");
            throw new JsonResponseException("province.id.is.null");
        }
        if (cityId == null) {
            log.error("city id is null");
            throw new JsonResponseException("city.id.is.null");
        }
        if (regionId == null) {
            log.error("region id is null");
            throw new JsonResponseException("region.id.is.null");
        }
    }

}
